package com.controller;

import com.alibaba.druid.util.StringUtils;
import com.service.UserService;
import com.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/active")
public class ActiveServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String code = req.getParameter("code");
        resp.setContentType("test/html;charset=utf-8");
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(code)) {
            //没有传递必须的参数
            resp.getWriter().write("玩呢，参数呢？");
            return;

        }

        //调用用户的业务。更新用户的状态
        try {

            int active = userService.active(username, code);
            if (active == 1) {
                resp.getWriter().write("激活成功,请开启您的被诈骗之旅");

            } else {
                resp.getWriter().write("激活失败,再重新试试,快点,我们着急诈骗你");

            }

        }catch (Exception e){
            e.printStackTrace();
            resp.sendRedirect("/error/error.jsp");

        }
    }
}